/*
 * @Descripttion: 
 * @version: V1.0.0
 * @Author: Maverick
 * @Date: 2025-03-29 00:05:10
 * @LastEditors: Maverick
 * @LastEditTime: 2025-03-29 20:23:55
 */
#include"sqlist.h"
#include<iostream>
#include<cstring>

namespace maverick{   
namespace sqlist{
template<typename  ElemType>
//构造函数
SqList<ElemType>::SqList(int size):maxSize(size),count(0)
{
    elems = new ElemType[maxSize];
    if(elems == nullptr)
    {
        std::cerr << "SqList::SqList(int size):" << "内存分配失败" << std::endl;
        exit(1);
    }
    std::cout << "SqList::SqList(int size):" << "内存分配成功" << std::endl;  
}

template<typename  ElemType>
//析构函数
SqList<ElemType>::~SqList()
{
    if(elems != nullptr)
    {
        delete []elems;
        elems = nullptr;
    }
    std::cout<<"SqList::~SqList():" << "内存释放成功" << std::endl;
    count = 0;
}

template<typename  ElemType>
//返回线性表元素个数
int SqList<ElemType>::Length() const
{
    return count;
}
//template parameter is not allowed to be used in the function definition
template<typename  ElemType>
//如果为空，返回true;否则返回false.
bool SqList<ElemType>::Empty() const
{
    return count == 0;
}

template<typename  ElemType>
//清空线性表
void SqList<ElemType>::Clear()
{
    count = 0;
    if(elems != nullptr)
    {
        delete []elems;
        elems = nullptr;
    }
    std::cout << "SqList::Clear:" << "线性表清空成功" << std::endl;
}

template<typename  ElemType>
//依次对线性表的每个元素调用函数visit
void SqList<ElemType>::Traverse(void (*visit)(const ElemType &)) const
{
    for(int i=0;i<count;++i)
    {
        (*visit)(elems[i]);
    }
    std::cout << "SqList::Traverse:" << "线性表遍历成功" << std::endl;

    
}


template<typename  ElemType>
//用e返回第i个位置的元素值.
bool SqList<ElemType>::GetElem(int position,ElemType &e) const
{
    if(position <0||position >=count)
    {
        std::cerr<<"SqList::GetElem(int position,ElemType &e):"<<"位置不合法"<<std::endl;
        return false;
    }
    e = elems[position];
    return true;
}


template<typename  ElemType>
//将线性表的第position个元素赋值为e.
bool SqList<ElemType>::SetElem(int position,const ElemType &e) 
{
    if(position <0||position >=count)
    {
        std::cerr<<"SqList::GetElem(int position,ElemType &e):"<<"位置不合法"<<std::endl;
        return false;
    }
    elems[position]=e;
    return true;
}

template<typename  ElemType>
//删除线性表的第i个位置的元素，并用e返回，长度减去1.
bool SqList<ElemType>::Delete(int position,ElemType &e) 
{
    if(position <0||position >=count)
    {
        std::cerr<<"SqList::GetElem(int position,ElemType &e):"<<"位置不合法"<<std::endl;
        return false;
    }
    e = elems[position];
    // 1 2 3 4 5 
    // 删除4 
    // 1 2 3 5
    for(int i=position;i<count-1;++i)
    {
        elems[i]=elems[i+1];
    }
    count--;
    return true;
}


template<typename  ElemType>
//删除元素
bool SqList<ElemType>::Delete(int position) 
{
    if(position <0||position >=count)
    {
        std::cerr<<"SqList::GetElem(int position,ElemType &e):"<<"位置不合法"<<std::endl;
        return false;
    }
    // 1 2 3 4 5 
    // 删除4 
    // 1 2 3 5
    for(int i=position;i<count-1;++i)
    {
        elems[i]=elems[i+1];
    }
    count--;
    return true;
}

template<typename  ElemType>
//插入元素
bool SqList<ElemType>::Insert(int position,const ElemType &e) 
{
    if(position <0||position >count)
    {
        std::cerr<<"SqList::GetElem(int position,ElemType &e):"<<"位置不合法"<<std::endl;
        return false;
    }
    if(count == maxSize)
    {
        std::cerr<<"SqList::Insert(int position,const ElemType &e):"<<"线性表已满"<<std::endl;
        return false;
    }
    // 1 2 3 4 5 
    // 插入6 
    // 1 2 3 4 5 6
    for(int i=count;i>position;--i)
    {
        elems[i]=elems[i-1];
    }
    elems[position]=e;
    count++;
    return true;
}


template<typename  ElemType>
//拷贝构造函数
SqList<ElemType>::SqList(const SqList<ElemType> &source)
{
    count = source.count;
    maxSize = source.maxSize;
    elems = new ElemType[maxSize];
    if(elems == nullptr)
    {
        std::cerr << "SqList::SqList(const SqList<ElemType> &source):" << "内存分配失败" << std::endl;
        exit(1);
    }
    std::cout << "SqList::SqList(const SqList<ElemType> &source):" << "内存分配成功" << std::endl;
    for(int i=0;i<count;++i)
    {
        elems[i] = source.elems[i];

    }
    std::cout << "SqList::SqList(const SqList<ElemType> &source):" << "拷贝构造函数成功" << std::endl;
    
}
template<typename  ElemType>
//拷贝赋值运算符重载
  
SqList<ElemType>& SqList<ElemType>::operator=(const SqList<ElemType> &source)
{
    if(this == &source)
    {
        return *this;
    }
    if(elems !=nullptr)
    {
        delete []elems;
        elems = nullptr;
    }
    count = source.count;
    maxSize = source.maxSize;
    elems = new ElemType[maxSize];
    if(elems == nullptr)
    {
        std::cerr << "SqList::operator=(const SqList<ElemType> &source):" << "内存分配失败" << std::endl;
        exit(1);
    }
    std::cout << "SqList::operator=(const SqList<ElemType> &source):" << "内存分配成功" << std::endl;
    for(int i=0;i<count;++i)
    {
        elems[i] = source.elems[i];
    }
    std::cout << "SqList::operator=(const SqList<ElemType> &source):" << "拷贝赋值运算符重载成功" << std::endl;
    return *this;
    
}
}//namespace sqlist
}// namespace maverick


// 显式实例化模板类
template class maverick::sqlist::SqList<double>;
template class maverick::sqlist::SqList<int>;
template class maverick::sqlist::SqList<char>;
template class maverick::sqlist::SqList<std::string>;
